package com.ujiuye.controller;

import com.ujiuye.pojo.Checkitem;
import com.ujiuye.pojo.Permission;
import com.ujiuye.service.IPermissionService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private IPermissionService permissionService;

    @PostMapping("/list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            return permissionService.listPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") int id){
        try {
            permissionService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @PostMapping
    public Result save(@RequestBody Permission permission){
        try {
            permissionService.save(permission);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @GetMapping("/{id}")//通过id查询检查项  用户修改时回显
    public Result getById(@PathVariable("id") int id){
        try {
            Permission permission = permissionService.getById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PutMapping//执行修改
    public Result update(@RequestBody Permission permission){
        try {
            permissionService.updateById(permission);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @GetMapping
    public Result listAll(){
        try {
            List<Permission> list = permissionService.list();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}

